package com.linjacy.httpproject.builder;

import com.linjacy.httpproject.enums.SqlJoinEnum;

import java.text.MessageFormat;
import java.util.Optional;
import java.util.Set;

public class SqlBuilder {

    private StringBuilder stringBuilder;

    public SqlBuilder(StringBuilder stringBuilder) {
        this.stringBuilder = stringBuilder;
    }

    public SqlBuilder buildSelect(Set<String> strings){
        strings.stream().forEach(key->{
            stringBuilder.append(",").append(key);
        });
        return this;
    }

    public SqlBuilder buildFromTable(String[] splitTableName, String[] joinWay, String[] joinField) {
        if(splitTableName.length == 1) {
            stringBuilder.append(" FROM ").append(splitTableName[0]);
        }else{
            for (int i = 0; i < joinWay.length; i++) {
                if(SqlJoinEnum.LEFT_JOIN.getCode().equals(joinWay[i])){
                    if(i == 0){
                        stringBuilder.append(" FROM ").append(splitTableName[0]).append(" ");
                    }
                    if(splitTableName.length >= (i+1)){
                        stringBuilder.append(MessageFormat.format(SqlJoinEnum.LEFT_JOIN.getJoinString(),splitTableName[i+1]))
                                .append(" ").append(joinField[i]);
                    }
                }else if(SqlJoinEnum.INNER_JOIN.getCode().equals(joinWay[i])){
                    if(i == 0){
                        stringBuilder.append(" FROM ").append(splitTableName[0]).append(" ");
                    }
                    if(splitTableName.length >= (i+1)){
                        stringBuilder.append(MessageFormat.format(SqlJoinEnum.INNER_JOIN.getJoinString(),splitTableName[i+1]))
                                .append(" ").append(joinField[i]);
                    }
                }else if(SqlJoinEnum.RIGHT_JOIN.getCode().equals(joinWay[i])){
                    if(i == 0){
                        stringBuilder.append(" FROM ").append(splitTableName[0]).append(" ");
                    }
                    if(splitTableName.length >= (i+1)){
                        stringBuilder.append(MessageFormat.format(SqlJoinEnum.RIGHT_JOIN.getJoinString(),splitTableName[i+1]))
                                .append(" ").append(joinField[i]);
                    }
                }
            }
        }
        return this;
    }

    public SqlBuilder buildGroup(String groupInfo) {
        Optional.ofNullable(groupInfo).ifPresent(s -> {
            stringBuilder.append(" GROUP BY ").append(s);
        });
        return this;
    }

    public SqlBuilder buildCondition(String conditionInfo) {
        Optional.ofNullable(conditionInfo).ifPresent(s -> {
            stringBuilder.append(" WHERE ").append(s);
        });
        return this;
    }

    public SqlBuilder buildOrder(String orderInfo) {
        Optional.ofNullable(orderInfo).ifPresent(s -> {
            stringBuilder.append(" ORDER BY ").append(s);
        });
        return this;
    }

    public String build(){
         return stringBuilder.toString().replaceFirst(",","SELECT ");
    }



}
